import os

from django.conf import settings
from django.shortcuts import render

from contents.models import ContentCategory
from qingmei_mall.utils import get_category


def generate_index_html():
    categorie = get_category.get_category()

    # 获取广告类别信息
    conten_categories = ContentCategory.objects.all()

    content_dict = {}
    for content in conten_categories:
        content_dict[content.key] = content.content_set.filter(status=True).order_by('sequence')

    context = {'categories': categorie,
               'contents': content_dict}
    # print(categorie[1]['sub_cats'])
    response= render(None, 'index.html', context) # response.content 响应对象，获取响应体
    file_path=os.path.join(settings.BASE_DIR,'static/index.html')
    with open(file_path,'w',encoding='utf-8') as f:
        f.write(response.content.decode())
